import 'package:flutter/material.dart';
import '../../model/space_item.dart';
import '../../spaces/image_space/recod_image_scan.dart';
import '../../widgets/ImgWidget.dart';
import '../../widgets/dialogWidget.dart';

//import 'dialogWidget.dart';

import '../../helper/cacheHelper.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../helper/routeHelper.dart';

import '../../model/user.dart';
import 'record_list.dart';
//import 'ImgWidget.dart';

class RecordCard {
  static Widget cardHeader(BuildContext context, User u1) {
    if (u1 == null) return new Container();
    return GestureDetector(
        // onTap: () => RouteHelper.UserInfo(context, u1),
        child: Container(
      child: new Row(
        children: <Widget>[
          new Container(
            child: new CircleAvatar(
              backgroundColor: Colors.blue,
              radius: 8.0,
            ),
            margin: EdgeInsets.only(right: 6.0),
          ),
          u1.Name != null
              ? new Container(
                  child: new Text(
                    u1.Name,
                    style: TextStyle(
                      // color: GlobalConfig.fontColor,
                      fontSize: 16.0,
                    ),
                  ),
                  margin: EdgeInsets.only(right: 12.0),
                )
              : new Container(),
        ],
      ),
      padding: const EdgeInsets.only(top: 10.0),
    ));
  }

  static Widget cardRemark(SpaceItem r1) {
    return new Container(
        child: r1.Remarks == null
            ? null
            : new Text(r1.Remarks,
                style:
                    new TextStyle(height: 1.3, color: GlobalConfig.fontColor)),
        margin: const EdgeInsets.only(top: 6.0),
        alignment: Alignment.topLeft);
  }

  static Widget cardTime(SpaceItem r1) {
    var t1 = DateTime.parse(r1.CreateTime);
    return new Container(
        child: new Text(t1 != null ? Helper.DateTimeToString(t1) : "",
            style: new TextStyle(height: 1.3, color: GlobalConfig.fontColor)),
        padding: const EdgeInsets.only(bottom: 8.0),
        alignment: Alignment.topLeft);
  }

  static Widget cardImg(
      BuildContext context, SpaceItem r1, RecordDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(r1.UserId);
    return GestureDetector(
        onTap: () {
          RouteHelper.ShowPage(context, RecordImageScanPage(r1));
        },
        onLongPress: () {
          if (u1 != null && u1.Guid != r1.UserId) {
            DialogWidget.getAlert(context, "提示", "只能删除自己上传的记录");
          } else {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return DialogWidget.getDialog(context, "删除照片", "点击确定键将删除该张照片！",
                    () {
                  SpaceItem.Delete(r1);
                  deleted(r1);
                });
              },
            );
          }
        },
        child: new Container(
            child: new AspectRatio(
              aspectRatio: 1.5 / 1.0,
              child: ImageWidget(r1.Guid),
            ),
            margin: new EdgeInsets.only(top: 6.0, bottom: 8.0),
            alignment: Alignment.topLeft));
  }

  static Widget ImgCard(
      BuildContext context, SpaceItem record, RecordDeleteCallback onDelete) {
    // var t1 = DateTime.parse(record.CreateTime);
    var u1 = CacheHelper.GetUser(record.UserId);

    return new Container(
        //margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        key: Key(record.Guid),
        margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        color: GlobalConfig.cardBackgroundColor,
        child: new Container(
          margin: new EdgeInsets.all(8.0),
          padding: new EdgeInsets.only(left: 4.0, right: 4.0),
          // onPressed: () {},
          child: new Column(
            children: <Widget>[
              cardHeader(context, u1),
              cardRemark(record),
              cardImg(context, record, onDelete),
              //MyImg(record.ImgId),
              // cardLocation(context, record),
              cardTime(record),
            ],
          ),
        ));
  }
}
